using Microsoft.AspNetCore.Mvc;
using ConsoleApi.Models;
using ConsoleApi.Services.Console;
using MESData.ErrorCode;

namespace ConsoleApi.Controllers
{
    /// <summary>
    /// 控制台仪表板控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class DashboardController : ControllerBase
    {
        private readonly IConsoleService _consoleService;

        public DashboardController(IConsoleService consoleService)
        {
            _consoleService = consoleService;
        }

        /// <summary>
        /// 获取今日核心指标
        /// </summary>
        [HttpGet("today-metrics")]
        public async Task<ApiResult<CoreMetricsDto>> GetTodayMetrics()
        {
            return await _consoleService.GetTodayCoreMetricsAsync();
        }

        /// <summary>
        /// 获取核心指标数据
        /// </summary>
        [HttpGet("core-metrics")]
        public async Task<ApiResult<List<CoreMetricsDto>>> GetCoreMetrics([FromQuery] DateTime? startDate, [FromQuery] DateTime? endDate)
        {
            return await _consoleService.GetCoreMetricsAsync(startDate, endDate);
        }

        /// <summary>
        /// 获取统计数据
        /// </summary>
        [HttpGet("statistics")]
        public async Task<ApiResult<List<StatisticsDto>>> GetStatistics([FromQuery] StatisticsQueryDto query)
        {
            return await _consoleService.GetStatisticsAsync(query);
        }

        /// <summary>
        /// 获取销售统计图表数据
        /// </summary>
        [HttpGet("sales-chart")]
        public async Task<ApiResult<ChartDataDto>> GetSalesChartData([FromQuery] string timeRange = "week")
        {
            return await _consoleService.GetSalesChartDataAsync(timeRange);
        }

        /// <summary>
        /// 获取采购统计图表数据
        /// </summary>
        [HttpGet("purchase-chart")]
        public async Task<ApiResult<ChartDataDto>> GetPurchaseChartData([FromQuery] string timeRange)
        {
            return await _consoleService.GetPurchaseChartDataAsync(timeRange);
        }



        /// <summary>
        /// 获取采购订单列表
        /// </summary>
        [HttpGet("purchase-orders")]
        public async Task<ApiResult<PagedResultDto<PurchaseOrderDto>>> GetPurchaseOrders([FromQuery] PurchaseOrderQueryDto query)
        {
            return await _consoleService.GetPurchaseOrdersAsync(query);
        }

        /// <summary>
        /// 获取生产工单列表
        /// </summary>
        [HttpGet("production-orders")]
        public async Task<ApiResult<PagedResultDto<ProductionOrderDto>>> GetProductionOrders([FromQuery] ProductionOrderQueryDto query)
        {
            return await _consoleService.GetProductionOrdersAsync(query);
        }

        /// <summary>
        /// 获取销售订单列表
        /// </summary>
        [HttpGet("sales-orders")]
        public async Task<ApiResult<PagedResultDto<ProductionSalesOrderDto>>> GetSalesOrders([FromQuery] ProductionSalesOrderQueryDto query)
        {
            return await _consoleService.GetProductionSalesOrdersAsync(query);
        }

        /// <summary>
        /// 获取审批单据列表
        /// </summary>
        [HttpGet("approval-documents")]
        public async Task<ApiResult<PagedResultDto<ApprovalDocumentDto>>> GetApprovalDocuments([FromQuery] ApprovalDocumentQueryDto query)
        {
            return await _consoleService.GetApprovalDocumentsAsync(query);
        }
    }
} 